package com.yff.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;



import com.yff.model.User;


@WebFilter("/*")
public class AuthorityFilter implements Filter {
	String encoding;
	@Override
	public void destroy() {
		this.encoding=null;
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
	//获取已经登录用户的信息
		HttpSession session=((HttpServletRequest)request).getSession();
		User loginUser=(User)session.getAttribute("loginInfo");
		String requestPath=((HttpServletRequest)request).getServletPath();
		if(requestPath.endsWith("index")
				||requestPath.contains("login")
				||loginUser.getRoleId()==1 && requestPath.contains("user")) {
			chain.doFilter(request, response);
		}else {

			RequestDispatcher dispatcher=request.getRequestDispatcher("index.jsp");
			dispatcher.forward(request,response);
		}
}


	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	

	}

}
